﻿using System.Linq;

namespace Sidvall.IdentityManager.Data.ConfigModel.EntityManagers
{
	public partial class ServerItemManager<TEntity, TList>
	{
		#region GetFreePluginId

		public int GetFreePluginId(IServerItem item)
		{
			int id, iMax;

			if (item == null)
				return 1;
			if (item.Plugins == null)
				return 1;
			var ids = (from o in item.Plugins
					   where o.PluginId != null
					   orderby o.PluginId
					   select o.PluginId.Value).ToList();
			iMax = ids.Count;
			for (int i = 0; i < iMax; i++)
			{
				id = ids[i];
				if (id != i + 1)
					return i + 1;
			}
			return iMax + 1;
		}

		#endregion
	}
}